<template>
  <div class="dashboard-container">
    <div class="dashboard-content animate__animated animate__fadeIn">
      <!-- 头部导航 -->
      <header class="dashboard-header">
        <div class="header-content">
          <h1 class="logo">用户系统</h1>
          <div class="user-menu">
            <span class="welcome-text">欢迎，{{ authStore.username }} ({{ userInfo.roleDescription }})</span>
            <button v-if="authStore.isAdmin" @click="goToAdmin" class="btn btn-primary admin-btn">
              管理控制台
            </button>
            <button @click="handleLogout" class="btn btn-secondary logout-btn">
              退出登录
            </button>
          </div>
        </div>
      </header>

      <!-- 主要内容区域 -->
      <main class="dashboard-main">
        <div class="welcome-section animate__animated animate__fadeInUp">
          <div class="welcome-card">
            <div class="welcome-icon">
              👋
            </div>
            <h2 class="welcome-title">{{ welcomeMessage }}</h2>
            <p class="welcome-subtitle">您已成功登录到系统</p>
            
            <div class="user-info">
              <div class="info-item">
                <span class="info-label">用户名：</span>
                <span class="info-value">{{ userInfo.username }}</span>
              </div>
              <div class="info-item">
                <span class="info-label">角色：</span>
                <span class="info-value">{{ userInfo.roleDescription }}</span>
              </div>
              <div class="info-item">
                <span class="info-label">注册时间：</span>
                <span class="info-value">{{ formatDate(userInfo.createdAt) }}</span>
              </div>
            </div>
          </div>
        </div>

        <!-- 功能卡片区域 -->
        <div class="features-section animate__animated animate__fadeInUp animate__delay-1s">
          <h3 class="section-title">系统功能</h3>
          <div class="features-grid">
            <div class="feature-card">
              <div class="feature-icon">👤</div>
              <h4 class="feature-title">个人资料</h4>
              <p class="feature-description">查看和编辑您的个人信息</p>
            </div>
            
            <div class="feature-card">
              <div class="feature-icon">🔒</div>
              <h4 class="feature-title">安全设置</h4>
              <p class="feature-description">管理您的账户安全选项</p>
            </div>
            
            <div class="feature-card">
              <div class="feature-icon">📊</div>
              <h4 class="feature-title">数据统计</h4>
              <p class="feature-description">查看您的使用统计信息</p>
            </div>
            
            <div class="feature-card">
              <div class="feature-icon">⚙️</div>
              <h4 class="feature-title">系统设置</h4>
              <p class="feature-description">配置系统参数和偏好</p>
            </div>
          </div>
        </div>
      </main>
    </div>

    <!-- 背景装饰 -->
    <div class="background-shapes">
      <div class="bg-shape bg-shape-1"></div>
      <div class="bg-shape bg-shape-2"></div>
      <div class="bg-shape bg-shape-3"></div>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { authAPI } from '@/api/auth'

export default {
  name: 'Dashboard',
  setup() {
    const router = useRouter()
    const authStore = useAuthStore()
    
    const welcomeMessage = ref('')
    const userInfo = ref({
      username: '',
      role: '',
      roleDescription: '',
      createdAt: null
    })
    
    const formatDate = (dateString) => {
      if (!dateString) return ''
      const date = new Date(dateString)
      return date.toLocaleDateString('zh-CN', {
        year: 'numeric',
        month: 'long',
        day: 'numeric',
        hour: '2-digit',
        minute: '2-digit'
      })
    }
    
    const loadWelcomeMessage = async () => {
      try {
        const response = await authAPI.getWelcomeMessage()
        welcomeMessage.value = response.data.message
      } catch (error) {
        console.error('获取欢迎信息失败:', error)
        welcomeMessage.value = `欢迎回来，${authStore.username}！`
      }
    }
    
    const loadUserInfo = async () => {
      try {
        const response = await authAPI.getCurrentUser()
        userInfo.value = response.data
      } catch (error) {
        console.error('获取用户信息失败:', error)
        userInfo.value.username = authStore.username
      }
    }
    
    const goToAdmin = () => {
      router.push('/admin')
    }
    
    const handleLogout = () => {
      authStore.logout()
      router.push('/login')
    }
    
    onMounted(async () => {
      await Promise.all([
        loadWelcomeMessage(),
        loadUserInfo()
      ])
    })
    
    return {
      authStore,
      welcomeMessage,
      userInfo,
      formatDate,
      goToAdmin,
      handleLogout
    }
  }
}
</script>

<style scoped>
.dashboard-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
  overflow: hidden;
}

.dashboard-content {
  position: relative;
  z-index: 2;
  min-height: 100vh;
}

.dashboard-header {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
  padding: 15px 0;
}

.header-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.logo {
  font-size: 24px;
  font-weight: 700;
  color: #333;
  margin: 0;
}

.user-menu {
  display: flex;
  align-items: center;
  gap: 15px;
}

.welcome-text {
  color: #666;
  font-weight: 500;
}

.admin-btn {
  padding: 8px 16px;
  font-size: 14px;
}

.logout-btn {
  padding: 8px 16px;
  font-size: 14px;
}

.dashboard-main {
  max-width: 1200px;
  margin: 0 auto;
  padding: 40px 20px;
}

.welcome-section {
  margin-bottom: 40px;
}

.welcome-card {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 20px;
  padding: 40px;
  text-align: center;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.welcome-icon {
  font-size: 48px;
  margin-bottom: 20px;
}

.welcome-title {
  font-size: 32px;
  font-weight: 700;
  color: #333;
  margin-bottom: 10px;
}

.welcome-subtitle {
  font-size: 18px;
  color: #666;
  margin-bottom: 30px;
}

.user-info {
  display: flex;
  justify-content: center;
  gap: 40px;
  flex-wrap: wrap;
}

.info-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
}

.info-label {
  font-size: 14px;
  color: #888;
  font-weight: 500;
}

.info-value {
  font-size: 16px;
  color: #333;
  font-weight: 600;
}

.features-section {
  margin-bottom: 40px;
}

.section-title {
  font-size: 24px;
  font-weight: 600;
  color: white;
  text-align: center;
  margin-bottom: 30px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.features-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 20px;
}

.feature-card {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  padding: 30px;
  text-align: center;
  transition: all 0.3s ease;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.feature-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
}

.feature-icon {
  font-size: 36px;
  margin-bottom: 15px;
}

.feature-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin-bottom: 10px;
}

.feature-description {
  font-size: 14px;
  color: #666;
  line-height: 1.5;
}

.background-shapes {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  z-index: 1;
}

.bg-shape {
  position: absolute;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 50%;
  animation: floatBackground 12s ease-in-out infinite;
}

.bg-shape-1 {
  width: 200px;
  height: 200px;
  top: 10%;
  left: -5%;
  animation-delay: 0s;
}

.bg-shape-2 {
  width: 300px;
  height: 300px;
  top: 60%;
  right: -10%;
  animation-delay: 4s;
}

.bg-shape-3 {
  width: 150px;
  height: 150px;
  bottom: 20%;
  left: 70%;
  animation-delay: 8s;
}

@keyframes floatBackground {
  0%, 100% {
    transform: translateY(0px) rotate(0deg);
  }
  33% {
    transform: translateY(-30px) rotate(120deg);
  }
  66% {
    transform: translateY(-60px) rotate(240deg);
  }
}

@media (max-width: 768px) {
  .header-content {
    flex-direction: column;
    gap: 15px;
  }
  
  .user-menu {
    flex-direction: column;
    gap: 10px;
  }
  
  .welcome-card {
    padding: 30px 20px;
  }
  
  .welcome-title {
    font-size: 24px;
  }
  
  .user-info {
    gap: 20px;
  }
  
  .features-grid {
    grid-template-columns: 1fr;
  }
}
</style>